﻿$("#ProjectName").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "/Project/Search",
            type: "POST",
            dataType: "json",
            data: {
                Keyword: request.term,
                ReturnType: "json"
            },
            success: function (projects) {
                response($.map(projects, function (project) {
                    return {
                        label: project.Name,
                        value: project.Name,
                        projectId: project.Id
                    }
                }));
            }
        });
    },
    minLength: 1,
    select: function (event, ui) {

        $("#ProjectId").val(ui.item.projectId);

        LoadProjectLookups();
    },
    open: function () {

        OnProjectChange();

        $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
    },
    close: function () {
        $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
    }
});
$("#OriginatorName").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "/Client/Search",
            type: "POST",
            dataType: "json",
            data: {
                Keyword: request.term,
                ReturnType: "json"
            },
            success: function (clients) {
                response($.map(clients, function (client) {
                    return {
                        label: client.FirstName + " " + client.LastName + " - " + client.Company + " - " + client.Email,
                        value: client.Company,
                        clientId: client.Id
                    }
                }));
            }
        });
    },
    minLength: 1,
    select: function (event, ui) {
        $("#Originator").val(ui.item.clientId);
    },
    open: function () {
        $("#Originator").val(null);
        $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
    },
    close: function () {
        $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
    }
});
$("#AssignedMemberName").autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "/Project/SearchAssignedMember",
            type: "POST",
            dataType: "json",
            data: {
                Keyword: request.term,
                ReturnType: "json",
                ProjectId: $("#ProjectId").val()
            },
            success: function (Accounts) {
                response($.map(Accounts, function (Account) {
                    return {
                        label: Account.FirstName + " " + Account.LastName + " - " + Account.Email,
                        value: Account.FirstName + " " + Account.LastName,
                        AccountId: Account.Id
                    }
                }));
            }
        });
    },
    minLength: 1,
    select: function (event, ui) {
        $("#AssignedMember").val(ui.item.AccountId);
    },
    open: function () {
        $("#AssignedMember").val(null);
        $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
    },
    close: function () {
        $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
    }
});

$("#loading").ajaxStart(function () {
    $(this).center();
    $(this).show();
});

$("#loading").ajaxStop(function () {
    $(this).center();
    $(this).hide();
});

function AddComboBoxItems(ComboBox, Items) {
    $.each(Items, function (Index, Item) {
        $(ComboBox).append(
            $('<option></option>').val(Item.Value).html(Item.Text)
        );
    });
    var SelectedId = "#" + $(ComboBox).attr("id") + "Selected";
    $(ComboBox).val($(SelectedId).val());
    $('.styledselect_form_1').selectbox({ inputClass: "styledselect_form_1" });
    $('.styledselect_form_1_error').selectbox({ inputClass: "styledselect_form_1_error" });
}

function ResetComboBoxItems(ComboBox) {
    $("option", ComboBox).remove();
    $(ComboBox).append(
            $('<option></option>').val("").html("Select " + $(ComboBox).attr('id'))
        );
    $('.styledselect_form_1').selectbox({ inputClass: "styledselect_form_1" });
    $('.styledselect_form_1_error').selectbox({ inputClass: "styledselect_form_1_error" });
}

function LoadProjectLookups() {
    $.ajax({
        url: "/Project/Lookups",
        type: "POST",
        dataType: "json",
        data: {
            ProjectId: $("#ProjectId").val(),
            ReturnType: "json"
        },
        success: function (Lookups) {

            $.each(Lookups, function (Index, Lookup) {

                var Items = $.map(Lookup.Items, function (Item) {
                    return {
                        Text: Item,
                        Value: Item
                    }
                });

                switch (Lookup.Name) {
                    case "Type":
                        AddComboBoxItems($("#Type"), Items);                        
                        break;
                    case "Version":
                        AddComboBoxItems($("#Version"), Items);
                        break;
                    case "Priority":
                        AddComboBoxItems($("#Priority"), Items);
                        break;
                    case "Status":
                        AddComboBoxItems($("#Status"), Items);
                        break;
                }

            });
        }
    });
}

$("#ProjectName").keyup(function (event) {
    $("#ProjectId").val(null);
    event.preventDefault();
});

$("#OriginatorName").keyup(function (event) {
    $("#Originator").val(null);
    event.preventDefault();
});

$("#AssignedMemberName").keyup(function (event) {
    $("#AssignedMember").val(null);
    event.preventDefault();
});

$("#ProjectName").focusout(function () {
    if ($(this).val() == "" || $(this).val() == null) {
        OnProjectChange();
    }
    if ($("#ProjectId").val() == null || $("#ProjectId").val() == "") {
        $(this).val("");
        OnProjectChange();
    }
});

$("#OriginatorName").focusout(function () {
    if ($(this).val() == "" || $(this).val() == null) {
        $("#Originator").val(null);
    }
    if ($("#Originator").val() == null || $("#Originator").val() == "") {
        $(this).val("");
    }
});

$("#AssignedMemberName").focusout(function () {
    if ($(this).val() == "" || $(this).val() == null) {
        $("#AssignedMember").val(null);
    }
    if ($("#AssignedMember").val() == null || $("#AssignedMember").val() == "") {
        $(this).val("");
    }
});


if ($("#ProjectId").val() != "" && $("#ProjectId").val() != null) {
    LoadProjectLookups();
}

function OnProjectChange() {
    $("#ProjectId").val(null);
    $("#Originator").val(null);
    $("#OriginatorName").val(null);
    $("#AssignedMember").val(null);
    $("#AssignedMemberName").val(null);

    $("#TypeSelected").val(null);
    $("#VersionSelected").val(null);
    $("#PrioritySelected").val(null);
    $("#StatusSelected").val(null);

    ResetComboBoxItems($("#Type"));
    ResetComboBoxItems($("#Version"));
    ResetComboBoxItems($("#Priority"));
    ResetComboBoxItems($("#Status"));
}

$("#ResetForm").click(function () {
    OnProjectChange();
    $("#ProjectName").val(null);
    $("#Title").val(null);
    $("#Description").val(null);
    $("#Diagnosis").val(null);
});